﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Role_Power_edit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            ddlRole.DataSource = dal.Role;
            ddlRole.DataBind();
            ddlPower.DataSource = dal.Power;
            ddlPower.DataBind();
            int id = int.Parse(Request["id"]);
            CMSModel.Role_Power role_power = dal.Role_Power.First(x => x.id == id);
            ddlRole.SelectedValue = role_power.RoleId.ToString();
            ddlPower.SelectedValue = role_power.Pow_Id.ToString();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        int id = int.Parse(Request["id"]);
        CMSModel.Role_Power role_power = dal.Role_Power.First(x => x.id == id);
        role_power.RoleId = int.Parse(ddlRole.SelectedValue);
        role_power.Role = dal.Role.First(x => x.RoleId == role_power.RoleId);
        role_power.Pow_Id = ddlPower.SelectedValue;
        role_power.Power = dal.Power.First(x => x.Id == role_power.Pow_Id);
        try
        {
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "edit", "alert('修改成功'),document.location='Default.aspx'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "editRole_Power", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "edit", "alert('修改失败')", true);
        }
    }
}